﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Crossroad.Utils.Extensions
{
    /// <summary>
    /// int类型扩展
    /// 编码：陈冉
    /// 2018-2-28 00:32:24
    /// </summary>
    public static class IntExt
    {
        /// <summary>
        /// 是否小于等于0。
        /// </summary>
        /// <returns></returns>
        [Description("是否小于等于0")]
        public static bool IsLessOrEqual0(this int input)
        {
            return input <= 0;
        }

        /// <summary>
        /// 是否不等于0。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Description("不等于0")]
        public static bool NotEqual0(this int input)
        {
            return input != 0;
        }

        /// <summary>
        /// 是否有内容
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Description("是否有内容 不是NULL 值不能等于0")]
        public static bool Contains(this int? input)
        {
            if (input == null)
                return false;

            return input.Value != 0;
        }

        /// <summary>
        /// 是否为空值 0 = 空
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Description("是否为空值 0 = 空")]
        public static bool IsEmpty(this int input)
        {
            return input == 0;
        }

        /// <summary>
        /// 无内容 == null || == 0
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Description("无内容 == null || == 0")]
        public static bool NotContent(this int? input)
        {
            if (input.IsNull())
                return true;

            return input == 0;
        }

        /// <summary>
        /// 时间戳转datetime类型
        /// </summary>
        /// <returns></returns>
        [Description("时间戳转datetime类型")]
        public static DateTime ToDateTime(this int input)
        {
            DateTime time = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 8, 0, 0), TimeZoneInfo.Local); // 当地时区

            return time.AddSeconds(Convert.ToDouble(input));
        }

        /// <summary>
        /// 数字日期转datetime 例：(20190101,yyyyMMdd)
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this int input, string format)
        {
            return DateTime.ParseExact(input.ToString(), format, null);
        }

        /// <summary>
        /// 时间戳转datetime类型字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Description("时间戳转datetime类型字符串")]
        public static string ToDateTimeStr(this int input)
        {
            if (input < 1)
                return "";

            return ToDateTime(input).ToDateTimeStr();
        }

        /// <summary>
        /// int转short
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Description("int转short")]
        public static short ToShort(this int input)
        {
            if (input.IsNull())
            {
                return 0;
            }

            return Convert.ToInt16(input);
        }

        /// <summary>
        /// int转换decimal格式
        /// </summary>
        /// <param name="input"></param>
        /// <param name="dividend"></param>
        /// <returns></returns>
        [Description("int转换decimal")]
        public static decimal ToDecimal(this int input, int? dividend = null)
        {
            if (input < 1)
                return 0m;

            var num = Convert.ToDecimal(input);
            if (!dividend.NotContent())
                num = num / dividend.Value;

            return  num;
        }

        /// <summary>
        /// int转byte
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Description("int转byte")]
        public static byte ToByte(this int input)
        {
            if (input < 1)
                return 0;

            return Convert.ToByte(input);
        }

        /// <summary>
        /// 转换枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        [Description("转换枚举")]
        public static T ToEnum<T>(this int input)
        {
            var type = typeof(T);

            return (T)Enum.ToObject(type, input);
        }


    }
}
